//
//  AppDelegate.m
//  ShaBaoOnline
//
//  Created by huang yong on 16/7/8.
//  Copyright © 2016年 com.ShaBaoOnline.www. All rights reserved.
//

#import "AppDelegate.h"
#import "Reachability.h"
#import "AFNetworking.h"
#import "AFURLRequestSerialization.h"
#import "STTabBarController.h"
#import "STLoginController.h"
#import "STNavigationController.h"
#import "GuideViewController.h"
#import "IQKeyboardManager.h"
#import "QJCheckVersionUpdate.h"
//shareSDK
//#import <ShareSDK/ShareSDK.h>
//#import <ShareSDKConnector/ShareSDKConnector.h>
////qq对应开放平台sdk头文件
//#import <TencentOpenAPI/TencentOAuth.h>
//#import <TencentOpenAPI/QQApiInterface.h>
////微信sdk头文件
//#import "WXApi.h"
////新浪微博sdk头文件
//#import "WeiboSDK.h"
BOOL isNetworkConnected = NO;
CGFloat HEIGHT_SCAL = 1.0 ;
CGFloat WIDTH_SCAL  = 1.0;
CGFloat TEXT_SIZE = 21;

@interface AppDelegate ()<UIAlertViewDelegate>

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [self setUp];
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    
    [self initNetwork];
    
    //引导图
    NSString *currentVersion = [NSBundle mainBundle].infoDictionary[@"CFBundleShortVersionString"];
    NSString *last_version = [[NSUserDefaults standardUserDefaults] valueForKey:@"lastVersion"];
    
    if (last_version == nil ||[last_version compare:currentVersion] == NSOrderedAscending) {
        [[NSUserDefaults standardUserDefaults] setValue:currentVersion forKey:@"lastVersion"];
        
        GuideViewController *guideVc = [[GuideViewController alloc] init];
        [self.window setRootViewController:guideVc];
        
    } else {
        STNavigationController *navVC = [[STNavigationController alloc] initWithRootViewController:[[STLoginController alloc] init]];
        
        self.window.rootViewController = navVC;
    }
    //键盘管理
    [self keyboard];
    [self openDataBase];
    [self.window makeKeyAndVisible];
   
    //适配
    ///////---------- APPI要改,,,,
   [self checkVerionUpdate];
    return YES;
}

/**
 *  检查版本更新
 */
- (void)checkVerionUpdate
{
    QJCheckVersionUpdate *update = [[QJCheckVersionUpdate alloc] init];
    [update showAlertView];
}

- (void)setUp{
    AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];
    
    
    
    if(SCREEN_HEIGHT > 480){
        HEIGHT_SCAL = SCREEN_HEIGHT / 568;
        WIDTH_SCAL = SCREEN_WIDTH / 320;
//        TEXT_SIZE = 20;
        myDelegate.autoSizeScaleX = SCREEN_WIDTH/320;
        
        myDelegate.autoSizeScaleY = SCREEN_HEIGHT/568;
        
    }else{
        
        myDelegate.autoSizeScaleX = 1.0;
        
        myDelegate.autoSizeScaleY = 1.0;
        
    }
    
    
}



- (void)keyboard{
    IQKeyboardManager *manager = [IQKeyboardManager sharedManager];
    
    manager.enable = YES;
    
    manager.shouldResignOnTouchOutside = YES;
    
    manager.shouldToolbarUsesTextFieldTintColor = YES;
    
    manager.enableAutoToolbar = NO;
}
//打开数据库并创建表
-(void)openDataBase{
   
}
- (void)initNetwork {
    
    Reachability *wifi = [Reachability reachabilityForLocalWiFi];
    Reachability *conn = [Reachability reachabilityForInternetConnection];
    
    if ([wifi currentReachabilityStatus] != NotReachable) { // 有wifi
        isNetworkConnected = YES;
        
    } else if ([conn currentReachabilityStatus] != NotReachable) { // 没有使用wifi, 使用手机自带网络进行上网
        isNetworkConnected = YES;
    } else {
        isNetworkConnected = NO;
    }
    
    AFNetworkReachabilityManager *mgr = [AFNetworkReachabilityManager sharedManager];
    [mgr setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        switch (status) {
            case AFNetworkReachabilityStatusUnknown: // 未知网络
                break;
                
            case AFNetworkReachabilityStatusNotReachable: // 没有网络(断网)
                isNetworkConnected = NO;
                break;
                
            case AFNetworkReachabilityStatusReachableViaWWAN: // 手机自带网络
                isNetworkConnected = YES;
                break;
                
            case AFNetworkReachabilityStatusReachableViaWiFi: // WIFI
                isNetworkConnected = YES;
                break;
        }
        
        [[NSNotificationCenter defaultCenter] postNotificationName:kChangeWift object:self];
    }];
    
    
    
    [mgr startMonitoring];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
//    STLoginController *loginVC = [[STLoginController alloc] init];
    STNavigationController *navVC = [[STNavigationController alloc] initWithRootViewController:[[STLoginController alloc] init]];
    [UIApplication sharedApplication].delegate.window.rootViewController = navVC;
}
- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    //关闭数据库
   
}

@end
